﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Graphically displays progres in progressbar
/// </summary>
public partial class ProgressBar : System.Web.UI.UserControl
{

    /// <summary>
    /// Width of progress bar
    /// </summary>
    private const int width = 30;

    /// <summary>
    /// Height of progress bar
    /// </summary>
    private const int height = 8;

    /// <summary>
    /// Color scale for displaying progress
    /// </summary>
    //private string[] colors = { "red", "yellow", "green" };
    private string[] colors = { "#FFFF00", "#CCFF33", "#99FF33", "#66FF33" };

    /// <summary>
    /// Color for progressbar when value is more than 100%
    /// </summary>
    private const string overflowColor = "red";

    /// <summary>
    /// Gets or sets the progress of the progress bar.
    /// </summary>
    /// <value>The progress.</value>
    public double Progress 
    {
        get
        {
            return progress;
        }
        set
        {
            int newProgress = System.Convert.ToInt32(value);
            int displayedProgress = newProgress > 100 ? 100 : newProgress;

            // skutocna sirka
            int realWidth = displayedProgress * width / 100;
            realWidth = realWidth == 0 && newProgress!=0 ? 1 : realWidth;
            this.CurrentProgress.Style.Add("width", String.Format("{0}px",realWidth));

            // farbicky
            string color = colors[(displayedProgress - 1) / ((100 / colors.Length) + 1)];
            color = newProgress > 100 ? overflowColor : color;
            this.CurrentProgress.Style.Add("background-color", color);

            // text
            this.TextProgress.Text = String.Format("{0}%", value.ToString());
            this.progress = newProgress;
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ProgressBarContainer.Width = width;
        this.ProgressBarContainer.Height = height;
        this.CurrentProgress.Height = height;
    }

    /// <summary>
    /// Progress of the progress bar.
    /// </summary>
    private int progress = 0;
}
